﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Text;
using PDFPARSERLib;

namespace PdfLib
{
    /// <summary>
    /// 
    /// </summary>
    public class PdfPropertyReader
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="pdfFilePath"></param>
        /// <returns></returns>
        public static PdfDocumentProperties GetPdfFileProperties(string pdfFilePath)
        {
            PDFPARSERLib.Document doc = new Document();
            if (doc.Open(pdfFilePath, string.Empty))
            {
                PdfDocumentProperties properties = new PdfDocumentProperties();
                properties.Keywords = doc.Keywords;
                properties.ModificationTime = doc.ModDate;
                properties.Author = doc.Author;
                properties.Title = doc.Title;
                properties.PageCount = doc.PageCount;
                properties.Producer = doc.Producer;
                properties.CreationTime = doc.CreationDate;
                properties.Subject = doc.Subject;
                doc.Close();
                return properties;
            }
            else
            {
                throw new Exception("Unable to open pdf file");
            }
        }

        ///<summary>
        ///</summary>
        ///<param name="filePath"></param>
        ///<returns></returns>
        ///<exception cref="Exception"></exception>
        public static string GetKeyWords(string filePath)
        {
            PDFPARSERLib.Document doc = new Document();
            if (doc.Open(filePath, string.Empty))
            {
                return doc.Keywords;
            }
            else
            {
                throw new Exception("Unable to open pdf file");
            }
        }

        public static DateTime GetCreationTime(string filePath)
        {
            PDFPARSERLib.Document doc = new Document();
            if (doc.Open(filePath, string.Empty))
            {
                return doc.CreationDate;
            }
            else
            {
                throw new Exception("Unable to open pdf file");
            }
        }

        public static DateTime GetModificationTime(string filePath)
        {
            PDFPARSERLib.Document doc = new Document();
            if (doc.Open(filePath, string.Empty))
            {
                return doc.ModDate;
            }
            else
            {
                throw new Exception("Unable to open pdf file");
            }
        }

        public static string GetAuthor(string filePath)
        {
            PDFPARSERLib.Document doc = new Document();
            if (doc.Open(filePath, string.Empty))
            {
                return doc.Author;
            }
            else
            {
                throw new Exception("Unable to open pdf file");
            }
        }

        public static string GetTitle(string filePath)
        {
            PDFPARSERLib.Document doc = new Document();
            if (doc.Open(filePath, string.Empty))
            {
                return doc.Title;
            }
            else
            {
                throw new Exception("Unable to open pdf file");
            }
        }

        ///<summary>
        ///</summary>
        ///<param name="filePath"></param>
        ///<returns></returns>
        ///<exception cref="Exception"></exception>
        public static int GetPageCount(string filePath)
        {
            PDFPARSERLib.Document doc = new Document();
            if (doc.Open(filePath, string.Empty))
            {
                return doc.PageCount;
            }
            else
            {
                throw new Exception("Unable to open pdf file");
            }
        }

        public static string GetProducer(string filePath)
        {
            PDFPARSERLib.Document doc = new Document();
            if (doc.Open(filePath, string.Empty))
            {
                return doc.Producer;
            }
            else
            {
                throw new Exception("Unable to open pdf file");
            }
        }

        public static string GetSubject(string filePath)
        {
            PDFPARSERLib.Document doc = new Document();
            if (doc.Open(filePath, string.Empty))
            {
                return doc.Subject;
            }
            else
            {
                throw new Exception("Unable to open pdf file");
            }
        }

        public static SizeF GetPageSize(string filePath, int pageNum)
        {
            SizeF size = new SizeF();
            PDFPARSERLib.Document pdfDoc = new PDFPARSERLib.Document();
            if (pdfDoc.Open(filePath, string.Empty))
            {
                pdfDoc.PageNo = pageNum;
                PDFPARSERLib.Page pdfPage = pdfDoc.Page;
                object[] dimensions = (object[])pdfPage.CropBox;
                if (dimensions != null && dimensions.Length == 4)
                {
                    float pageHeight = float.Parse(dimensions[3].ToString());
                    float pageWidth = float.Parse(dimensions[2].ToString());
                    size = new SizeF(pageWidth, pageHeight);
                }
                pdfDoc.Close();
            }
            pdfDoc = null;
            return size;
        }

        public static PageOrentation GetPageOrentation(string filePath, int pageNum)
        {
            PageOrentation orentation = PageOrentation.Portrait;
            PDFPARSERLib.Document pdfDoc = new PDFPARSERLib.Document();
            if (pdfDoc.Open(filePath, string.Empty))
            {
                pdfDoc.PageNo = pageNum;
                PDFPARSERLib.Page pdfPage = pdfDoc.Page;
                if (pdfPage.Rotate == 90 || pdfPage.Rotate == 270)
                {
                    orentation = PageOrentation.Landscape;
                }
                pdfDoc.Close();
            }
            pdfDoc = null;
            return orentation;
        }
    }
}
